package org.example.pictools.controller;

import org.example.pictools.service.PicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class PicController {


	@Autowired
	PicService picService;
	@PostMapping("/picAll")
	public List<Map<String,String>> resPicEx(@RequestParam("list") List<String> list){
		StringBuilder folderName = new StringBuilder();
		list.forEach(val -> {
			folderName.append(val).append("/");
		});
		File file=new File(folderName.toString());
		File[] array = file.listFiles();
		List<Map<String,String>> list2=new ArrayList<>();
		for(int i=0;i<array.length;i++){
			if(array[i].isFile()) {
				File file2 = new File(array[i].getPath());
				list2.add(picService.picS(file2));
			}
		}
		return  list2;
	}

	@PostMapping("/picOne")
	public Map<String,String> res(@RequestParam("list") List<String> list,@RequestParam("fileName") String fileName){
		StringBuilder folderName = new StringBuilder();
		list.forEach(val -> {
			folderName.append(val).append("/");
		});

		File file=new File(folderName.toString()+"\\"+fileName);
		return  picService.picO(file);
	}
}
